<?php
/**
 * 
 * IndexAction.class.php (前台首页)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2011-03-01 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class ShareAction extends BaseAction
{
    function _initialize()
    {	
        parent::_initialize();
        if(!$this->_userid){
            $this->assign('jumpUrl',U('User/Login/index'));
            $this->error(L('nologin'));
            exit;
        }
        $this->dao = M('Share');
        $this->assign('bcid',0);
        $user = M('User')->find($this->_userid);
        $this->assign('vo',$user);
        unset($_POST['status']);
        unset($_POST['groupid']);
        unset($_POST['amount']);
        unset($_POST['point']);
        $_GET =get_safe_replace($_GET);
        $count1 = M('Order')->where("userid=".$this->_userid)->count();
        $count2 = M('Order')->where("userid=".$this->_userid." and status<>0 and pay_status<>0 and shipping_status<>0")->count();
        $count3 = M('Order')->where("userid=".$this->_userid." and status=0 and pay_status=0 and shipping_status=0")->count();
        $count4 = M('Order')->where("userid=".$this->_userid." and status=4")->count();
        $this->assign('count1',$count1);  
        $this->assign('count2',$count2);  
        $this->assign('count3',$count3);  
        $this->assign('count4',$count4);
        $messagecount = M("User_message")->where("touserid=".$this->_userid." or touserid=0")->count();
        $this->assign('messagecount',$messagecount);
        if($this->_groupid==7){
            $sjcount1 = M('Order')->where("sjuserid=".$this->_userid)->count();
            $sjcount2 = M('Order')->where("sjuserid=".$this->_userid." and status=2")->count();
            $sjcount3 = M('Order')->where("sjuserid=".$this->_userid." and status<2 and (pay_status=2 or pay_status=3) and (shipping_status=0 or shipping_status=3)")->count();
            $sjcount4 = M('Order')->where("sjuserid=".$this->_userid." and status=5")->count();
            $sjcount5 = M('Order')->where("sjuserid=".$this->_userid." and status<2 and pay_status=3 and shipping_status=3")->count();
            $this->assign('sjcount1',$sjcount1);  
            $this->assign('sjcount2',$sjcount2);  
            $this->assign('sjcount3',$sjcount3);  
            $this->assign('sjcount4',$sjcount4);
            $this->assign('sjcount5',$sjcount5);
        }
    }
    
    public function index()
    {
        $count = $this->dao->where("userid=".$this->_userid)->count();
        if($count){
            import ( "@.ORG.Page" );
            $listRows =  10;
            $page = new Page ( $count, $listRows );
            $pages = $page->show();
            $field =  $this->module[$this->mod[$module]]['listfields'];
            $field =  "*";
            $list = $this->dao->field($field)->where("userid=".$this->_userid)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();
            $this->assign('pages',$pages);
            $this->assign('list',$list);
        }
        $this->assign('menuWZ',21);
        $this->display();
		
    }
    public function massage()
    {
        $this->assign('menuWZ',32);
        $this->display();
     }
    public function proshare()
    {
        $jgtable=array();
            $jgtable['jg_product']="p";
            $jgtable['jg_share']="s";
            $jgtable_field="p.*";
            $jgtable_sql='p.userid='.$this->_userid." and p.status=1 and p.id=s.proid";
            $prolist = M()
                ->table($jgtable)
                ->field($jgtable_field)
                ->where($jgtable_sql)
                ->select();
        $proidlist="";
        foreach ($prolist as $k=>$r){
            if($k==0){
                $proidlist=$r["id"];
            }
            else{
                $proidlist.=",".$r["id"];
            }
        }
        $proid =  intval($_REQUEST['pid']);
        $where="id in(".$proidlist.")";
        $count = M("Product")->where($where)->count();
        if($count){
            import ( "@.ORG.Page" );
            $listRows =  12;
            $page = new Page ( $count, $listRows );
            $pages = $page->show();
            $field =  $this->module[$this->mod[$module]]['listfields'];
            $field =  "*";
            $list = M("Product")->field($field)->where($where)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();
            foreach ($list as $k=>$r){
                $list[$k]["sharecount"]=  $this->dao->where("proid=".$r["id"])->count();
            }
            $this->assign('pages',$pages);
            $this->assign('list',$list);
        }
        $this->assign('menuWZ',10);
        $this->display();		
    }
    public function usershare()
    {
        $prolist=M("Product")->where("userid=".$this->_userid." and status=1")->select();
        $proidlist="";
        foreach ($prolist as $k=>$r){
            if($k==0){
                $proidlist=$r["id"];
            }
            else{
                $proidlist.=",".$r["id"];
            }
        }
        $proid =  intval($_REQUEST['pid']);
        $where="proid in(".$proidlist.")";
        if($proid>0){
            $where=" proid=".$proid;
            $this->assign('menuWZ',10);
        }
        else{
            $this->assign('menuWZ',11);
        }
        $count = $this->dao->where($where)->count();
        if($count){
            import ( "@.ORG.Page" );
            $listRows =  12;
            $page = new Page ( $count, $listRows );
            $pages = $page->show();
            $field =  $this->module[$this->mod[$module]]['listfields'];
            $field =  "*";
            $list = $this->dao->field($field)->where($where)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();
            $this->assign('pages',$pages);
            $this->assign('list',$list);
        }
        $this->assign('proid',$proid);
        $this->display();		
    }
 
    public function add($id='',$module='')
    {
        $id =  intval($_REQUEST['id']);
        if($id>0){
            //判断是否是会员购买过的
            $isbuy = M("order")->where("shipping_status=2 and product_id=".$id)->find();
            if($isbuy){
                $isshare=$this->dao->where("userid=".$this->_userid." and proid=".$id)->count();
                if($isshare>0){
                    $this->error("抱歉,您已经对此商品分享过了,不能重复分享.");
                }
                else{
                    $this->assign('jgpo',$isbuy);
                }
            }
            else{
                $this->error("抱歉,您还没有购买过此商品,或者您的交易还没有完成.");
            }            
        }
        $jgsat_auth_key = sysmd5(C('ADMIN_ACCESS').$_SERVER['HTTP_USER_AGENT']);
        $jgsat_auth = authcode('0-1-0-1-jpeg,jpg,png,gif-3-0', 'ENCODE',$jgsat_auth_key);
        $this->assign('jgsat_auth',$jgsat_auth);
        $jgsat_auth2 = authcode('0-1-0-10-jpeg,jpg,png,gif-3-0', 'ENCODE',$jgsat_auth_key);
        $this->assign('jgsat_auth2',$jgsat_auth2);
        $this->assign('menuWZ',25);
        $this->display(); 
    }
    public function edit($id='',$module='')
    {
        
        $id =  intval($_REQUEST['id']);
        $share=$this->dao->find($id);
        if($share["proid"]>0){
            $pro=M("Product")->find($share["proid"]);
            $this->assign('pro',$pro);
        }
        if(!empty($share["pics"])){
            $p_data=explode(':::',$share["pics"]);
            $share["pics"]=array();
            foreach($p_data as $k=>$res){
                $p_data_arr=explode('|',$res);					
                $share["pics"][$k]['filepath'] = $p_data_arr[0];
                $share["pics"][$k]['filename'] = $p_data_arr[1];
            }
            unset($p_data);
            unset($p_data_arr);
        }
        
        $this->assign('sh',$share);
        $jgsat_auth_key = sysmd5(C('ADMIN_ACCESS').$_SERVER['HTTP_USER_AGENT']);
        $jgsat_auth = authcode('0-1-0-1-jpeg,jpg,png,gif-3-0', 'ENCODE',$jgsat_auth_key);
        $this->assign('jgsat_auth',$jgsat_auth);
        $jgsat_auth2 = authcode('0-1-0-10-jpeg,jpg,png,gif-3-0', 'ENCODE',$jgsat_auth_key);
        $this->assign('jgsat_auth2',$jgsat_auth2);
        $this->assign('menuWZ',25);
        $this->display(); 
    }
    
    public function insert()
    {
        if(empty($_POST["title"]))$this->error("主题不能为空!");
        if(empty($_POST["thumb"]))$this->error("请上传封面图片!");
        if(empty($_POST["content"]))$this->error("内容不能为空!");
        
        if(!empty($_POST["proid"])){
            $_POST["status"]=1;
            $jgpro=M("Product")->find($_POST["proid"]);
            $_POST["catid"]=$jgpro["catid"];
        }  
//        else{
//            $_POST["status"]=1;
//        }
        if(file_exists($_SERVER['DOCUMENT_ROOT'].$_POST["thumb"])){
            //$jgimage=  explode(".", $_POST["thumb"]);
            $jgsrc_img=$_SERVER['DOCUMENT_ROOT'].$_POST["thumb"];
            
            $jgimg_x=$_POST["hideX1"];    //起点X
            $jgimg_y=$_POST["hideY1"];    //起点Y
            $jgimg_w=$_POST["hideWidth"]; //宽度W
            $jgimg_h=$_POST["hideHeight"];//高度H
            //$jgsrcthumb=$_SERVER['DOCUMENT_ROOT'].$jgimage[0]."_thumb".".".$jgimage[1];
            my_image_resize($jgsrc_img, $jgsrc_img ,$jgimg_x,$jgimg_y, $jgimg_w , $jgimg_h);

        }
        else{
            $this->success("图片裁剪出错,图片可能被删除了!");
        }
        $_POST["status"]=  $this->sharestatus;
        //$_POST["thumb"]=$_POST["pics"][1];
        if(!$this->dao->create($_POST)) {
            $this->error($this->dao->getError());
        }
        
        foreach ($_POST["pics"] as $key=>$val){
            if($key>0){
                $imgstr="";
                $imgstr=$val."|".$_POST["pics_name"][$key];
                if($key==1){
                    $this->dao->pics=$imgstr;
                }
                else{
                    $this->dao->pics.=":::".$imgstr;
                }                
            }
        }
        //print_r($this->dao);exit;
        $this->dao->userid = $this->_userid;
        $this->dao->username = $this->_username;
        $this->dao->createtime = time();
        $this->dao->updatetime = time();
        $this->dao->lang = 1;
        $result	=$this->dao->add();
        if(false !== $result) {
            $data['id']= $result;
            $data['url']= U('Home/Share/show?id='.$result);
            $this->dao->save($data);
            if($_POST["status"]==1){
                $r =M('User')->where("id =".$userid)->setInc('point',$this->sharepoint);
                addUserDetail("分享获得积分",1,  $this->sharepoint,$this->_userid,$this->_username,1,$result,1);
            }
            $this->assign ( 'jumpUrl', U('Share/index') );
            $this->success("分享发布成功!");
        }else{
            $this->error("分享发布失败,请认真录入!");
        }
    }
    
    public function update()
    {
        if(empty($_POST["title"]))$this->error("主题不能为空!");
        if(empty($_POST["thumb"]))$this->error("请上传封面图片!");
        if(empty($_POST["content"]))$this->error("内容不能为空!");
        
        if(file_exists($_SERVER['DOCUMENT_ROOT'].$_POST["thumb"])){
            //$jgimage=  explode(".", $_POST["thumb"]);
            $jgsrc_img=$_SERVER['DOCUMENT_ROOT'].$_POST["thumb"];
            
            $jgimg_x=$_POST["hideX1"];    //起点X
            $jgimg_y=$_POST["hideY1"];    //起点Y
            $jgimg_w=$_POST["hideWidth"]; //宽度W
            $jgimg_h=$_POST["hideHeight"];//高度H
            //$jgsrcthumb=$_SERVER['DOCUMENT_ROOT'].$jgimage[0]."_thumb".".".$jgimage[1];
            my_image_resize($jgsrc_img, $jgsrc_img ,$jgimg_x,$jgimg_y, $jgimg_w , $jgimg_h);

        }
        else{
            $this->success("图片裁剪出错,图片可能被删除了!");
        }
        //$_POST["thumb"]=$_POST["pics"][1];
        if(!$this->dao->create($_POST)) {
            $this->error($this->dao->getError());
        }
        
        foreach ($_POST["pics"] as $key=>$val){
                $imgstr="";
                $imgstr=$val."|".$_POST["pics_name"][$key];
                if($key==0){
                    $this->dao->pics=$imgstr;
                }
                else{
                    $this->dao->pics.=":::".$imgstr;
                }    
        }
        //print_r($this->dao);exit;
        $result	=$this->dao->save();
        if(false !== $result) {
            $this->assign ( 'jumpUrl', U('Share/index') );
            $this->success("分享修改成功!");
        }else{
            $this->error("分享修改失败,请认真录入!");
        }
    }
    public function jgdel()
    {
        $id= intval($_REQUEST['id']);
        $result=$this->dao->where("id=".$id." and userid=".$this->_userid)->delete();
        if ($result) {
            die(json_encode(array('msg'=>"删除成功!")));
        }else{
            die(json_encode(array('msg'=>"删除失败")));
        }
    }
    public function jgdel2()
    {
        $id= intval($_REQUEST['id']);
        $result=$this->dao->where("id=".$id." and status=0")->delete();
        if ($result) {
            die(json_encode(array('msg'=>"删除成功!")));
        }else{
            die(json_encode(array('msg'=>"只能删除没审核的分享.")));
        }
    }
    public function jgstatus()
    {
        $id= intval($_REQUEST['id']);
        $jgtable=array();
            $jgtable['jg_share']="s";
            $jgtable['jg_product']="p";
            $jgtable_field="s.*";
            $jgtable_sql='p.id=s.proid and s.id='.$id.' and p.userid='.$this->_userid;
            $isbuy = M()
                ->table($jgtable)
                ->field($jgtable_field)
                ->where($jgtable_sql)
                ->find();
        if($isbuy){
            //$data["id"]=$id;
            if($isbuy["status"]==1){
                $data["status"]=0;
            }
            else{
                $data["status"]=1;
            }
            $r=$this->dao->where("id=".$id)->save($data);
            if($r){
                die(json_encode(array('msg'=>"审核状态更改成功!")));
            }else{
                die(json_encode(array('msg'=>"审核状态更改失败!")));
            }
        }
    }
}
?>